$(function () {
    var key = "";
    var field = "id";//页面加载时  默认按照id排序
    var ordertype = "asc";//页面加载时  默认升序排序
    var page = 1;//页面加载时  默认显示第一页
    var shownum = 160;//页面加载时  默认每页显示5条
    yemian()
    async function yemian() {
        var result = await goodsziduan({ key, field, ordertype, page, shownum })
        var { status, msg, sql } = result;
        console.log(result);
        if (status) {
            if (sql.length) {
                var html = "";
                sql.forEach(({ goodsId, goodsName, goodsImg, goodsPrice }) => {
                    html += `<li class="more2_item more2_item_good hover-on">
              <a href="../html/gooddetail.html?gid=${goodsId}"> 
              <div class="o2_impr"></div>
               <a class="more2_lk" href="../html/gooddetail1.html?gid=${goodsId}"
                   target="_blank" title="${goodsName}" aria-label="${goodsName}"
                   clstag="h|keycount|feed|01#pd_049-a">
                   <div class="mod_lazyload_placeholder" style="height: 220px;">
                       <img
                           src="https:${goodsImg}"
                           class="lazyimg_img"
                           alt="${goodsName}">   
                   </div>
                   <div class="more2_info">
                       <p class="more2_info_name"><i class="more2_info_self">自营</i>${goodsName}</p>
                       <div class="more2_info_price more2_info_price_plus more2_info_price_newcomer">
                           <div class="mod_price"><i>¥</i><span class="more2_info_price_txt">${String(goodsPrice).split(".")[0]}.<span
                                       class="more2_info_price_txt-decimal">${String(goodsPrice).split(".")[1] == undefined ? 0 : String(goodsPrice).split(".")[1]}</span></span></div>
                       </div>
                   </div>
               </div>
               </a>
               </a>
           </li>`
                })
                $(".more2_list").html(html);
            }
        }
    }

    function zuzhi(ele) {
        if (ele.preventDefault) {
            ele.preventDefault();
        }
        else {
            ele.returnValue = false;
        }
    }
    $(".a1").click(function (event) {
        var event = event || window.event;
        zuzhi(event);
    })



    var wrapwidth = $(".wrap").width()
    var index = 0;//全局变量  记录轮播的下标
    var timer = null;

    $(".wrapnav li").click(function () {
        // var list = Array.from($(".nav li"))
        // index = list.indexOf(this)
        index = $(this).index()
        console.log(index);
        dianji();
    })



    function autoplay() {
        clearInterval(timer)
        timer = setInterval(function () {
            index++;
            dianji();
        }, 3000)
    }

    autoplay();//页面加载时,默认启动轮播

    $(".wrap").mouseenter(function () {
        clearInterval(timer)
    })
    $(".wrap").mouseleave(function () {
        autoplay();
    })

    $(".wrapright").click(function () {
        // 判断box是否正在运动
        if ($(".box").is(":animated")) return false
        index++;
        dianji();
    })
    $(".wrapleft").click(function () {
        if ($(".box").is(":animated")) return false
        index--;
        dianji();
    })


    function dianji() {


        // 左边临界值
        if (index < 0) {
            index = $(".wrapnav li").length;//第五张下标
            $(".box").stop().css({ left: -wrapwidth * index })
            index--;//之后调用move()  滚动到第四张
        }


        // 右边临界值
        if (index > $(".wrapnav li").length) {//下标超出第五张
            index = 0;             //下标切换
            $(".box").stop().css({ left: 0 })
            index++;//之后调用move()  滚动到第二张
        }
        var activeindex = index >= $(".wrapnav li").length ? 0 : index
        // listli[activeindex].className = "l1";

        $(".wrapnav li").stop().eq(activeindex).addClass("l1").siblings().removeClass("l1")
        $(".box").stop().animate({ left: -wrapwidth * index }, 1000)
    }

    if (document.cookie) {
        if (getcookie("lgc")) {
            $(".show").css({ display: "none" }).removeClass()
            $(".hide").css({ display: "block" })
            $(".hide a").eq(0).html(`欢迎你:${getcookie("lgc")}`)
        }
    }


    $(".a1").click(function () {
        deletecookie("lgc")
        location.reload();
    })

    $(".searchBtn").click(function () {
        if ($(".searchCon").val()) {
            location.href = `./goodslist.html?key=${$(".searchCon").val()}`
        }
    })

    $(document).on("click", ".shoptocar", async function () {
        location.href = "./shopcar.html"
    })



})